//
// Created by tianq on 24-12-10.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n = 0;
    vector<int> numbers;

    cin >> n;
    for (int i = 0; i < n; i++) {
        int tmp;
        cin >> tmp;
        numbers.push_back(tmp);
    }

    // ideology
    // by merging smallest 2 each time, we get the smallest score
    // by merging greatest 2 each time, we get the greatest score
    // don't just take my words, try it on paper

    vector<int> minNums = numbers, maxNums = numbers;
    int minScore=0, maxScore = 0;

    ranges::sort(minNums);
    while (minNums.size() > 1) {
        int newNum = minNums[0] + minNums[1];
        minScore += newNum;

        minNums.erase(minNums.begin(), minNums.begin() + 2);
        auto it = ranges::lower_bound(minNums, newNum);
        minNums.emplace(it, newNum);
    }

    ranges::sort(maxNums, greater());
    while (maxNums.size() > 1) {
        int newNum = maxNums[0] + maxNums[1];
        maxScore += newNum;

        maxNums.erase(maxNums.begin(), maxNums.begin()+2);
        // the product of two biggest numbers should still be biggest
        maxNums.emplace(maxNums.begin(), newNum);
    }

    cout << maxScore - minScore << endl;

}

